package flavor;

/*
 * Copyright (c) 1997-2004 Alexandros Eleftheriadis, Danny Hong and 
 * Yuntai Kyong.
 *
 * This file is part of Flavor, developed at Columbia University
 * (http://flavor.sourceforge.net).
 *
 * Flavor is free software; you can redistribute it and/or modify
 * it under the terms of the Flavor Artistic License as described in
 * the file COPYING.txt. 
 *
 */

/*
 * Authors:
 * Alexandros Eleftheriadis <eleft@ee.columbia.edu>
 * Yuntai Kyong <yuntaikyong@ieee.org>
 * Danny Hong <danny@ee.columbia.edu>
 *
 */



import java.io.*;


/**
 * FlIOException is the base class for all the exceptions generated by
 * any implementation of the IBitstream interface
 */
public class FlIOException extends IOException 
{
	private static int NUMBER_OF_EXCEPTION_TYPE = 10;
	
	/**
	 * End of data (exception condition)
	 */
	public static int ENDOFDATA = 0;
	
	/**
	 * Invalid alignment for align() method (exception condition)
	 */
	public static int INVALIDALIGNMENT = 1;
	
	/**
	 * The (little_)getxxx() or (little_)nextxxx() method failed (exception condition)
     */
	public static int READFAILED = 2;
	
	/**
	 * The (little_)putxxx() method failed (exception condition)
	 */
	public static int WRITEFAILED = 3;
	
	/**
	 * File open failed (exception condition)
	 */
	public static int FILEOPENFAILED = 4;
	
	/**
	 * System I/O operation failed (exception condition)
	 */
	public static int SYSTEMIOFAILED = 5;
	
	/**
	 * Unknown I/O type (exception condition)
	 */
	public static int INVALIDIOTYPE = 6;
	
	/**
	 * Invalid bitsize specified (exception condition)
	 */
	public static int INVALIDBITSIZE = 7;
	
	/**
	 * Not enough data in bitstream (exception condition)
	 */
	public static int NOTENOUGHDATA = 8;
	
	/**
	 * Unknown reason (exception condition)
	 */
	public static int UNKNOWN = 9;
	
	
	private int reason = UNKNOWN;
	private String system_error_msg = "";
	
	private static String errormsg[] =  {
		"End of Data",
		"Invalid Alignment",
		"Read I/O Failed",
		"Write I/O Failed",
		"File Open Failed",
		"System I/O Error",
		"Invalid Input Type",
		"Invalid bit size",
		"Not enough data",
		"Unknown Error"
	};
	
	/**
	 * Create FlIOException 
	 */
	public FlIOException() {
		super();
	}
	
	/**
	 * Create FlIOException with a description
	 */
	public FlIOException(String s) {
		super(s);
	}
	
	/**
	 * Create FlIOException with an exception condition
	 * @param error_code The error code representing the exception condition
	 */
	public FlIOException(int error_code) {
		super(errormsg[error_code>=NUMBER_OF_EXCEPTION_TYPE?NUMBER_OF_EXCEPTION_TYPE-1:error_code]);
		reason = error_code>=NUMBER_OF_EXCEPTION_TYPE?NUMBER_OF_EXCEPTION_TYPE-1:error_code;
	}
	
	/**
	 * Create FlIOException with an exception condition
	 * @param error_code The error code representing the exception condition
	 * @param error_msg Additional error description
	 */
	public FlIOException(int error_code, String error_msg) {
		super(errormsg[error_code>NUMBER_OF_EXCEPTION_TYPE?NUMBER_OF_EXCEPTION_TYPE-1:error_code]);
		reason = error_code>=NUMBER_OF_EXCEPTION_TYPE?NUMBER_OF_EXCEPTION_TYPE-1:error_code;
		system_error_msg = error_msg;
	}
	
	/**
	 * Convert an exception to a string
	 * @return Descriptive message about the error
	 */
	public String toString()  {
		if(reason != SYSTEMIOFAILED) 
			return super.toString();
		else
			return super.toString() + "\n" + system_error_msg;	
	}
}
